<?php
namespace Home\Controller;
use Common\Controller\BaseController;
class PermitController extends BaseController 
{
    public $action = 0;
    public $page_size = 10;

    public function _initialize()
    {
        parent::_initialize();
        self::$_action_block = 2;
        $this->assign('block',self::$_action_block);
    }

    public function index()
    {
        $this->action = 2;
        $permitModel = D('Permit');

        $where = " staff.staff_id={$this->staff_id} and permit.is_drift=1";
        $count = $permitModel
                ->join('staff on staff.staff_id=permit.staff_id','LEFT')
                ->where($where)
                ->count();

        vendor('Page');
        $Page  = new \Vendor\Page($count,$this->page_size);
        
        $field = "permit.*,staff.staff_name ";
        $list = $permitModel->field($field)
                ->order('apply_time DESC')
                ->join('staff on staff.staff_id=permit.staff_id','LEFT')
                ->where($where)
                ->limit($Page->firstRow.','.$Page->listRows)
                ->select();
        $this->assign('list',$list);
        
        $this->assign('page',$Page->show());
        $this->assign('type',$this->action);
        $this->display('index');   //显示index页面
    }
    

    public function add()    //对应view中的add 3个页面
    {
        $this->action = 1;
        $step = !empty($_GET['step']) && (intval($_GET['step'])>1) ? intval($_GET['step']) : 1 ;
        $staff = $this->getUserInfo();
        $this->assign('staff',$staff);
        $this->assign('type',$this->action);
        $this->assign('now',date('Y-m-d H:i:s'));
        $this->display('add'.$step);
    }

    public function save($drift=0)
    {
        $post = I('post.');
        $permitModel = D('Permit');

        if (1==$post['special_cer'] && empty($post['special_id'])) {
            $this->error('请同填写证号');
        }

        $image_path = $this->fileUpload();
        $post['is_drift'] = $drift ? 2 : 1;
        if (!$image_path['status']) {
            $this->error($image_path['info']);
        }
        $post ['operator_photo'] = $image_path['info']['operator_photo'];

        $res =  $permitModel->addPermit($post);
        if(false !== $res['status']) {
            $this->success($res['info'],U('Home/Permit/index'));
        }
        else {
            $this->error($res['info']);
        }
        exit;
    }

    public function drift()
    {
        $this->action = 3;
        $permitModel = D('Permit');

        $where = " staff.staff_id={$this->staff_id} and permit.is_drift=2";
        $count = $permitModel
            ->join('staff on staff.staff_id=permit.staff_id','LEFT')
            ->where($where)
            ->count();

        vendor('Page');
        $Page  = new \Vendor\Page($count,$this->page_size);

        $field = "permit.*,staff.staff_name ";
        $list = $permitModel->field($field)
            ->order('apply_time DESC')
            ->join('staff on staff.staff_id=permit.staff_id','LEFT')
            ->where($where)
            ->limit($Page->firstRow.','.$Page->listRows)
            ->select();
        $this->assign('list',$list);

        $this->assign('page',$Page->show());
        $this->assign('type',$this->action);
        $this->display('drift');   //显示index页面
    }

    public function edit()
    {
        $id     = I('get.id',0,'intval');
        $type   = I('get.type',0,'intval');
        $where  = " permit.is_drift=2 and permit.id={$id}";
        $permitModel  = D('Permit');
        $field = " permit.*,staff.*";
        $info = $permitModel
            ->field($field)
            ->join('staff on permit.staff_id=staff.staff_id')
            ->where($where)
            ->select();
        if(empty($info))
        {
            $this->error('数据不存在');
        }

        $this->assign('info',$info[0]);
        $this->display('edit');
    }

    public function delete()
    {
      $this->display('delete');
    }

    public function getUserInfo()
    {
        $staff_id = $this->staff_id;
        // $field = "";
        $staffModel = D('Staff');
        $staff = $staffModel->getStaffById($staff_id);
        return $staff;
    }

    public function view()
    {
        $id     = I('get.id',0,'intval');
        $type   = I('get.type',0,'intval');  
        $where  = " permit.is_drift=1 and permit.id={$id}";
        $permitModel  = D('Permit');
        $field = " permit.*,staff.*";
        $info = $permitModel
                ->field($field)
                ->join('staff on permit.staff_id=staff.staff_id')
                ->where($where)
                ->select();

        if(empty($info))
        {
            $this->error('数据不存在');
        }
        $this->assign('info',$info[0]);
        $view   = 'view_'.$type; 
        $this->display($view);
    }
    public function judge()
    {
        $id       = I('post.permit_id',0,'intval');
        $status   = I('post.status',1,'intval'); 
        $remark   = I('post.remark','','htmlspecialchars');  
        $permitModel  = D('Permit');
        $res = $permitModel->where("id={$id}")->select();
    
        if(empty($res))
        {
            $this->error('数据不存在');
        }

        $data = array(
        'id'    =>$id,
        'status'=>$status,
        'remark'=>$remark
        );

        $res = $permitModel->save($data);
        if(false !== $res)
        {
            $this->success('操作成功',U('Admin/Permit/index'));
        }
        else
        {
            $this->error('操作失败');
        }
    }
    public function fileUpload()
    {
        $return = array('status' => '', 'info' => '');
        $path = C('FILE_UPLOAD_PATH');
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 31457282;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = $path; // 设置附件上传根目录
//        $upload->savePath  =     date('Ymd'); // 设置附件上传（子）目录
        // 上传文件
        if (!is_dir($path)) {
            mkdir($path, 0777, true);
        }

        $info = $upload->upload();
        if (!$info) {// 上传错误提示错误信息
            $return['status'] = false;
            $return['info'] = $upload->getError();
        } else {// 上传成功
            $return['status'] = true;
            $return['info']['operator_photo'] = $info['photo']['savepath'].$info['photo']['savename'];
        }
        return $return;
    }
 
}